package com.weiqiao.mall.repository.impl;

import com.google.gson.Gson;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import com.weiqiao.base.dao.impl.BaseMongoDaoImpl;
import com.weiqiao.mall.domain.ChannalProductEntity;
import com.weiqiao.mall.domain.ProductEntity;
import com.weiqiao.mall.repository.IProductRepository;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 产品管理 后台配置 持久化层 实现层 继承IProductDaoImpl
 */
@Repository("ProductRepositoryImpl")
public class ProductRepositoryImpl extends BaseMongoDaoImpl implements IProductRepository {

    private String collectionName = "product";

    @Override
    public List<String> getList(int company_id) {
        Bson filter1 = Filters.eq("company_id", company_id);
        List<String> list = this.findFilter(collectionName, filter1);
        return list;
    }

    /**
     * 产品管理
     * 通过company_id查询产品信息
     */
    @Override
    public List<ProductEntity> getProductList(int company_id) {
        Gson gsonz = new Gson();
        Bson filter1 = Filters.eq("company_id", company_id);
        List<String> list = this.findFilter(collectionName, filter1);
        List<ProductEntity> proList = new ArrayList<ProductEntity>();
        for (String fan1 : list) {
            ProductEntity ben1 = gsonz.fromJson(fan1, ProductEntity.class);
            proList.add(ben1);
        }
        return proList;
    }

    @Override
    public String getOneProduct(Bson filter) {
        String result = this.findOne(collectionName, filter);
        return result;
    }

    /**
     * 产品管理 --添加 修改产品
     */
    @Override
    public boolean editProductByJson(Bson filter, String json) {
        return this.updateReplace(collectionName, filter, json);
    }

    /**
     * 产品管理 --添加 修改产品
     */
    @Override
    public void editProductByDocument(Bson filter, Document doc) {
        this.updateOne(collectionName, filter, doc, true);
    }

    /**
     * 产品管理--搜索
     *
     * @param company_id
     * @param pro_nam
     */
    @Override
    public List<ProductEntity> searchProZ(int company_id, String pro_nam) {
        Pattern pattern = Pattern.compile("^.*" + pro_nam + ".*$", Pattern.CASE_INSENSITIVE);
        Gson gsonz = new Gson();
        Bson filter1 = Filters.eq("pro_nam", pattern);
        Bson filter2 = Filters.eq("company_id", company_id);
        Bson filter0 = Filters.and(filter1, filter2);
        List<String> list = this.findFilter(collectionName, filter0);
        List<ProductEntity> serList = new ArrayList<ProductEntity>();
        for (String fan1 : list) {
            ProductEntity ben1 = gsonz.fromJson(fan1, ProductEntity.class);
            serList.add(ben1);
        }

        return serList;
    }

    /**
     * 产品管理--单项删除
     *
     * @param company_id
     * @param pro_id
     */
    @Override
    public boolean delOnlyOneZ(int company_id, String pro_id) {
        Bson filter1 = Filters.eq("company_id", company_id);
        Bson filter2 = Filters.eq("pro_id", pro_id);
        Bson filter0 = Filters.and(filter1, filter2);
        return this.deleteOne(collectionName, filter0);
    }

    /**
     * 产品管理--删除选中产品
     *
     * @param company_id
     * @param pro_id
     */
    @Override
    public boolean delSelectProZ(int company_id, String[] pro_id) {
        Bson filter1 = Filters.eq("company_id", company_id);
        Bson filter2 = Filters.in("pro_id", pro_id);
        Bson filter0 = Filters.and(filter1, filter2);
        return this.delMany(collectionName, filter0);
    }

    /**
     * 订单列表--查询产品信息
     *
     * @param company_id
     * @param pro_nam
     */
    @Override
    public List<ProductEntity> findProByDet(int company_id, String pro_nam) {
        Gson gsonz = new Gson();
        Bson filter1 = Filters.eq("company_id", company_id);
        Bson filter2 = Filters.eq("pro_nam", pro_nam);
        Bson filter0 = Filters.and(filter1, filter2);
        List<String> list = findFilter(collectionName, filter0);
        List<ProductEntity> proList = new ArrayList<ProductEntity>();
        for (String a : list) {
            ProductEntity pro = gsonz.fromJson(a, ProductEntity.class);
            proList.add(pro);
        }

        return proList;
    }

    @Override
    public int countProduct(int company_id, Bson bson) {
        MongoCollection<Document> dbCollection = getCollection(collectionName);
        return (int) dbCollection.count(bson);
    }

    /**
     * 分页查询功能优化
     */
    @Override
    public List<String> getProductsByFilterSortSkipLimt(Bson filter, Bson filterSort, int skip, int limit) {
        return this.findFilterSortSkipLimit(collectionName, filter, filterSort, skip, limit);
    }

    @Override
    public ProductEntity findOneProduct(int company_id, Bson filter) {
        String a = this.findOne(collectionName, filter);
        ProductEntity pro = new Gson().fromJson(a, ProductEntity.class);
        return pro;
    }

    @Override
    public void updateOneProduct(int company_id, Bson bson, Document doc1) {
        MongoCollection<Document> dbCollection = getCollection(collectionName);
        dbCollection.updateMany(bson, new Document("$set", doc1));
    }

    @Override
    public List<String> getProductListBySort(int company_id, Bson sort) {
        Bson filter = Filters.and(Filters.eq("company_id", company_id), Filters.eq("del_flag", 0));
        return this.findFilterSort(collectionName, filter, sort);
    }

    @Override
    public boolean insertOneProduct(String json) {
        return this.inSert(collectionName, json);
    }

    @Override
    public List<ProductEntity> getMany(Bson filter, Bson sort) {
        List<String> list = this.findFilterSort(collectionName, filter, sort);
        ArrayList<ProductEntity> proList = new ArrayList<ProductEntity>();
        if (!list.isEmpty()) {
            for (String a : list) {
                ProductEntity pro = new Gson().fromJson(a, ProductEntity.class);
                proList.add(pro);
            }
        }
        return proList;
    }

    @Override
    public ProductEntity getOne(Bson filter) {
        String findOne = this.findOne(collectionName, filter);
        return new Gson().fromJson(findOne, ProductEntity.class);
    }

    @Override
    public List<ChannalProductEntity> findProducts(Bson filter) {
        List<String> list = this.findFilter(collectionName, filter);
        List<ChannalProductEntity> prodoctlist = new LinkedList<ChannalProductEntity>();
        for (String item : list) {
            prodoctlist.add(new Gson().fromJson(item, ChannalProductEntity.class));
        }

        return prodoctlist;
    }

}
